package com.battery.service.impl;

import com.battery.dao.PowerBankDao;
import com.battery.pojo.PowerBank;
import com.battery.service.Power_BankService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

@Service
public class Power_BankServiceImpl implements Power_BankService {
    @Autowired
    private PowerBankDao powerBankDao;
    //private DeviceDao deviceDao;
    @Override
    public Result addPowerBank(PowerBank powerbank){
        try {
            if(powerBankDao.findByPb_Id(powerbank.getPb_id())!=null)
                return Result.error().message("该电源设备已经存在");
            else
                powerBankDao.addByPb_Id(powerbank);
        } catch (DuplicateKeyException e){
            return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("添加成功");

    }
    @Override
    public Result deletePowerBank(int pb_id){
        try {
            if(powerBankDao.findByPb_Id(pb_id)!=null)
                powerBankDao.deleteByPb_Id(pb_id);
            else
            return Result.error().message("该电源设备不存在");
 //       } catch (DuplicateKeyException e){
 //           return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("删除成功");

    }
    @Override
    public Result updatePowerBank(PowerBank powerbank){
        try {
            if(powerBankDao.findByPb_Id(powerbank.getPb_id())!=null)
                powerBankDao.updatePowerBank(powerbank);
            else
            return Result.error().message("该电源设备不存在");
 //       } catch (DuplicateKeyException e){
//            return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("更新成功");

    }
    @Override
    public Result findPowerBank(int pb_id){
        try {
            if(powerBankDao.findByPb_Id(pb_id)!=null)
                powerBankDao.findByPb_Id(pb_id);
            else
            return Result.error().message("该电源设备不存在");
//        } catch (DuplicateKeyException e){
//            return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("查询成功");

    }
    public Result searchPowerBank(int device_id,int pb_location){
        try {
            if(powerBankDao.findByPb_Id(device_id)==powerBankDao.findByPb_Id(pb_location))
                powerBankDao.findByPb_Id(device_id);
            else
                return Result.error().message("该电源设备不存在");
//        } catch (DuplicateKeyException e){
//            return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("查询成功");

    }
}
